package com.xhwl.centiotdata.pollingtrigger.talkback;

import cn.hutool.core.util.ObjectUtil;
import com.xhwl.centiotdata.handler.talk.TalkIntegrationFactory;
import com.xhwl.centiotdata.handler.talk.TalkIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.talkback.TalkBackIntegrationConfigDTO;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.TalkBackIntegrationConfig;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.enums.device.DeviceTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: wuyuhao
 * @createDate: 2022/10/31 18:01
 */
@Slf4j
@Service
public class TalkBackDispatcher implements ScheduleDispatcher {

    @Autowired
    private TalkBackIntegrationConfig integrationConfig;

    @Override
    public void doOneHourTask() {
        // 云对讲获取云对讲设备
        ThreadPoolUtils.executor.execute(this::executePullTalkBackDevice);
    }

    public void executePullTalkBackDevice() {
        // 获取到需要执行拉取云对讲设备的项目及配置
        List<TalkBackIntegrationConfigDTO> integrationConfigDTOList = integrationConfig.getIntegrationConfig(null);
        log.info("获取的云对讲集成配置为:{}", integrationConfigDTOList);
        integrationConfigDTOList.forEach(configDTO -> {
            log.info("configDTO: {}", configDTO);
            try {
                log.info("开始拉取：{}", configDTO.getProductId());
                TalkIntegrationHandler handler = TalkIntegrationFactory.getInvokeStrategy(configDTO.getProductId());
                if (ObjectUtil.isEmpty(handler)) {
                    return;
                }
                configDTO.setDeviceType(DeviceTypeEnum.TALK_BACK.getCode());
                configDTO.setCategoryEnum(CategoryEnum.TALK_BACK_SYSTEM);
                handler.pullDevice(configDTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    @Override
    public void doOneMinutesTask() {
        // 云对讲获取对讲记录
        ThreadPoolUtils.executor.execute(this::executePullTalkLog);
    }

    private void executePullTalkLog() {
        // 获取到需要执行拉取人行设备的项目及配置
        List<TalkBackIntegrationConfigDTO> integrationConfigDTOList = integrationConfig.getIntegrationConfig(null);
        log.info("获取的云对讲集成配置为:{}", integrationConfigDTOList);
        integrationConfigDTOList.forEach(integrationConfigDTO -> {
            log.info("integrationConfigDTO: {}", integrationConfigDTO);
            try {
                log.info("开始拉取：{}", integrationConfigDTO.getProductId());
                TalkIntegrationHandler handler = TalkIntegrationFactory.getInvokeStrategy(integrationConfigDTO.getProductId());
                if (ObjectUtil.isEmpty(handler)) {
                    return;
                }
                handler.pullTalkLog(integrationConfigDTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
